if SERVER then AddCSLuaFile( "AR2Dance.lua" ) end

local GM = IMiniGame.New();
GM:Derive("BasePVP")

GM["Info"] = {
	["Name"] = "AR2Dance",
	["Version"] = "1.0",
	["up_date"] = "22 November 2010",
	["Author"] = {
		["Name"] = "CowThing",
		["Email"] = "",
		["URL"] = ""
	},
	["Info"] = "Fly around, and kill.",
	["Description"] = "Firing your AR2 sends you flying, keep moving and kill\neveryone you meet.\nFirst player to 10 kills wins!",
	["Icon"] = "gui/silkicons/lightning"
}

GM.WinSound = Sound("common/stuck1.wav")
GM.LoseSound = Sound("items/suitchargeok1.wav")

/*--------------------------------------
	OnPreLoad
--------------------------------------*/
function GM.Functions.OnPreLoad()

	for k,v in pairs( player.GetAll() ) do
		v:SetFrags( 0 )
		v:SetHealth(v:GetMaxHealth())
		v.NextJump = CurTime()
	end
	return true
	
end

/*--------------------------------------
	OnUnload
--------------------------------------*/
function GM.Functions.OnUnload()

	for k,v in pairs( player.GetAll() ) do
		v:SetFrags( 0 )
		v:SetHealth(v:GetMaxHealth())
		v.NextJump = nil
	end
	return true
	
end

/*--------------------------------------
	MiniGame Functions
--------------------------------------*/
function GM.Hooks.PlayerLoadout( pl )

	pl:GiveAmmo(256,"AR2",true)
	pl:Give("weapon_ar2")
	return true
	
end

function GM.Hooks.EntityTakeDamage( victim, weapon, attacker, amnt, dmginfo )
	
	--Any shot will take 100 HP
	if victim:IsPlayer() and attacker:IsPlayer() then
		dmginfo:ScaleDamage(1)
		dmginfo:SetDamage(100)
	end
	
end

function GM.Functions.CheckForWinner(killer)
	
	--Check for winner
	if killer:Frags() >= 10 then
		PrintMessage(HUD_PRINTCENTER, killer:Nick().." wins!")
		PrintMessage(HUD_PRINTTALK, killer:Nick().." wins!")
		for _,ent in pairs(player.GetAll()) do
			if killer == ent then
				ent:SendLua("surface.PlaySound(\""..GM.WinSound.."\")")
			else
				ent:SendLua("surface.PlaySound(\""..GM.LoseSound.."\")")
			end
		end
		timer.Simple(5,function() IMiniGame.Unload() end)
	end
	
end

function GM.Hooks.PlayerDeath( victim, weapon, killer )
	
	--Kill rewards
	if killer:IsPlayer() then
		killer:SetHealth(killer:Health()+100)
		killer:GiveAmmo(128,"AR2",true)
	end
	
	timer.Simple(0,function(GM,killer)GM.Functions.CheckForWinner(killer)end,GM,killer)
	
end

function GM.Hooks.Think( )
	
	--Flying
	for _,ply in pairs(player.GetAll()) do
		if (ply.NextJump <= CurTime()) and ply:KeyDown(IN_ATTACK) and (ply:GetActiveWeapon():Clip1() > 0) then
			ply.NextJump = CurTime()+0.1
			ply:SetVelocity(ply:GetAimVector()*-150)
		end
	end
	
end

function GM.Hooks.HUDPaint()
	
	local text = "Frags\n"..LocalPlayer():Frags().."/10"
	draw.SimpleTextOutlined(text, "Trebuchet24", ScrW()/2, 40, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
	
end

function GM.Hooks.GetFallDamage() return 0 end --No Fall Damage
function GM.Hooks.PlayerHurt( pl ) end --Uneeded function that was defined in BasePVP

GM:Register()